package safari.controller.transaction;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import safari.model.member.MemberVO;
import safari.model.sell.SellBoardVO;
import safari.model.transaction.SellBoardTransactionException;
import safari.model.transaction.TransactionService;

public class TransactionController extends MultiActionController {
	private TransactionService transactionService;
	public TransactionController(TransactionService transactionService){
		this.transactionService=transactionService;
	}
	/**
	 * 구매게시판에서 포인트로 상품구매하는 메서드
	 * @param request
	 * @param response
	 * @param svo
	 * @return
	 * @throws SQLException 
	 * @throws SellBoardTransactionException 
	 */
	public ModelAndView transaction(HttpServletRequest request, HttpServletResponse response, 
			HttpSession session, SellBoardVO svo) throws SQLException {
		String resultMessage="";
		MemberVO mvo=(MemberVO)session.getAttribute("userInfo");
		String buyerId=mvo.getId();
		String sellerId=svo.getMvo().getId();
		if(buyerId.equals(sellerId))
			resultMessage="본인 아이디로 본인의 물건을 구매할 수 없습니다.";
		else
			try {
				mvo=transactionService.transaction(buyerId, sellerId, svo);
				session.setAttribute("userInfo",mvo);
				resultMessage="정상 거래 되었습니다.";
			} catch (SellBoardTransactionException e) {
				System.out.println(e.getMessage());
				resultMessage=e.getMessage();
			}		
		return new ModelAndView("sell/sell_transaction_result","resultMessage",resultMessage);
	}
}
